import Vue from 'vue'
export default {
    getImgSizeByUrl(url){
        return new Promise((resolve, reject) => {
            var img = new Image()
            img.onload = () => {
                resolve({
                    
                    width: img.width,
                    height: img.height
                })
            }
            img.src = url
        })
    },
    getFileSize(file){
        return new Promise((resolve, reject) => {
            var reader = new FileReader()
            reader.readAsDataURL(file)
            reader.onload = (e) => {
                var dataUrl = reader.result
               
                var img = new Image()
                img.onload = () => {
                    resolve({
                        size: file.size,
                        width: img.width,
                        height: img.height
                    })
                }
                img.src = dataUrl
            }
        })
    },
    async checkImgForHuaWei(file){
        Vue.$vux.loading.show({
            text: '加载中'
          })
        var res
        if (typeof file == 'string') {
        res = await this.getImgSizeByUrl(file)
        } else {
            res = await this.getFileSize(file)
        }

        Vue.$vux.loading.hide();
        
        if (res.size > 1024 * 1024 * 8) {
            return {
                flag: false,
                msg: '图片不能超过8M'
            }
        }
        if (res.width < 120 || res.height < 120) {
            return {
                flag: false,
                msg: '图片太小，图片宽高最少 120 x 120'
            }
        }
        // if (res.width > 4000 || res.height > 2000) {
        //     return {
        //         flag: false,
        //         msg: '图片太大，图片宽高最大 4000 x 2000'
        //     }
        // }
        return {
            flag: true,
            msg: ''
        }
    }
}